cmake_minimum_required(VERSION 3.5.0)

# 获取当前所在平台
if(CMAKE_HOST_WIN32)
    set(WINDOWS 1)
elseif(CMAKE_HOST_APPLE)
    set(MACOS 1)
elseif(CMAKE_HOST_UNIX)
    set(LINUX 1)
endif()

# 强制使用C++11标准
set(CMAKE_C_STANDARD 99)
set(CMAKE_CXX_STANDARD 14) 

# 指定需要的最低MacOS版本
if(MACOS)
    set(CMAKE_OSX_DEPLOYMENT_TARGET "10.15" CACHE STRING "支持的最低MacOS版本" FORCE)
endif()

# Windows项目添加Unicode支持，避免中文乱码
if(WINDOWS)
    add_definitions("-DUNICODE" "-D_UNICODE")
endif()
add_definitions(-DEXPORT_CLASS)

# 设置不同编译方式下的优化方式
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
    set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -Wall -o0")
elseif(CMAKE_BUILD_TYPE STREQUAL "Release")
    set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -Wall -o3")
endif()

project(BaseTool)

# ShareLib's path
if(WINDOWS)
    set(ShareLibPath ${CMAKE_CURRENT_SOURCE_DIR}/ShareLib/Win)
else()
    set(ShareLibPath ${CMAKE_CURRENT_SOURCE_DIR}/ShareLib/Unix)
endif()
message(STATUS "ShareLib=" ${ShareLibPath})

# Include the head files
include_directories(${PROJECT_SOURCE_DIR}
    ${PROJECT_SOURCE_DIR}/Tool
    ${PROJECT_SOURCE_DIR}/Tool/Logger
    ${ShareLibPath}/OpenSSL/include
    ${ShareLibPath}/Poco/include)

# Link poco library
if(WINDOWS)
    if(${CMAKE_BUILD_TYPE} STREQUAL "Debug")
        set(OpensslLibs crypto.dll ssl.dll)
        set(PocoLibs PocoCryptod.dll
            PocoDatad.dll
            PocoDataSQLited.dll
            PocoEncodingsd.dll
            PocoFoundationd.dll
            PocoJSONd.dll
            PocoNetd.dll
            PocoNetSSLd.dll
            PocoUtild.dll
            PocoXMLd.dll
            PocoZipd.dll)
    else()
        set(OpensslLibs crypto.dll ssl.dll)
        set(PocoLibs PocoCrypto.dll
            PocoData.dll
            PocoDataSQLite.dll
            PocoEncodings.dll
            PocoFoundation.dll
            PocoJSON.dll
            PocoNet.dll
            PocoNetSSL.dll
            PocoUtil.dll
            PocoXML.dll
            PocoZip.dll)
    endif()
elseif(MACOS)
    set(SpdLoglibs libspdlog.a)
    if(${CMAKE_BUILD_TYPE} STREQUAL "Debug")
        set(OpensslLibs libcrypto.3.dylib libssl.3.dylib)
        set(PocoLibs libPocoCryptod.95.dylib
            libPocoDatad.95.dylib
            libPocoDataSQLited.95.dylib
            libPocoEncodingsd.95.dylib
            libPocoFoundationd.95.dylib
            libPocoJSONd.95.dylib
            libPocoNetd.95.dylib
            libPocoNetSSLd.95.dylib
            libPocoUtild.95.dylib
            libPocoXMLd.95.dylib
            libPocoZipd.95.dylib)
    else()
        set(OpensslLibs libcrypto.3.dylib libssl.3.dylib)
        set(PocoLibs libPocoCrypto.95.dylib
            libPocoData.95.dylib
            libPocoDataSQLite.95.dylib
            libPocoEncodings.95.dylib
            libPocoFoundation.95.dylib
            libPocoJSON.95.dylib
            libPocoNet.95.dylib
            libPocoNetSSL.95.dylib
            libPocoUtil.95.dylib
            libPocoXML.95.dylib
            libPocoZip.95.dylib)
    endif()
endif()

link_directories(${ShareLibPath}/OpenSSL/lib)
link_directories(${ShareLibPath}/SpdLog/lib)
if(WINDOWS)
    link_directories(${ShareLibPath}/Poco/lib/${CMAKE_GENERATOR_PLATFORM}/${CMAKE_BUILD_TYPE})
elseif(MACOS)
    link_directories(${ShareLibPath}/Poco/lib/${CMAKE_BUILD_TYPE})
endif()
link_libraries(${OpensslLibs} ${PocoLibs} ${SpdLoglibs})

# Source group 
set(BaseEventSrc Tool/BaseEvent/IBaseEvent.h
    Tool/BaseEvent/BaseEvent.h
    Tool/BaseEvent/EventBase.h
    Tool/BaseEvent/BaseEvent.cpp)
set(FoundationSrc Tool/Foundation/Foundation.h
    Tool/Foundation/Foundation.cpp)
set(ModuleLoaderSrc Tool/ModuleLoader/ModuleConfig.h)
set(CryptoSrc Tool/Crypto/BCrypto.h
    Tool/Crypto/BCrypto.cpp)
set(DateTimeSrc Tool/DateTime/DateTime.h
    Tool/DateTime/DateTime.cpp)
set(DirectorySrc Tool/Directory/Directory.h
    Tool/Directory/Directory.cpp)
set(DllLoaderSrc Tool/DllLoader/DllLoader.h
    Tool/DllLoader/DllLoader.cpp)
set(EncodingSrc Tool/Encoding/Encoding.h
    Tool/Encoding/Encoding.cpp)
set(EventSrc Tool/Event/BEvent.h
    Tool/Event/BEvent.cpp)
set(FileSrc Tool/File/File.h
    Tool/File/File.cpp)
set(GuidSrc Tool/GUID/Guid.h
    Tool/GUID/Guid.cpp)
set(IniSrc Tool/Ini/Ini.h
    Tool/Ini/Ini.cpp)
set(JsonSrc Tool/Json/JsonDoc.h
    Tool/Json/JsonDoc.cpp)
set(LoggerSrc Tool/Logger/Logger.h
    Tool/Logger/Logger.cpp
    Tool/Logger/SystemLogger.h)
set(PocoLoggerSrc Tool/PocoLogger/PocoLogger.h
    Tool/PocoLogger/PocoLogger.cpp
    Tool/PocoLogger/SysLogger.h)
set(OTSrc Tool/OT/IObserver.h
    Tool/OT/ITarget.h
    Tool/OT/Target.h
    Tool/OT/TargetBase.h
    Tool/OT/Target.cpp)
set(PipeSrc Tool/Pipe/NamedPipe.h
    Tool/Pipe/NamedPipe.cpp)
set(ProcessSrc Tool/Process/Process.h
    Tool/Process/Process.cpp)
set(SemaphoreSrc Tool/Semaphore/BSemaphore.h
    Tool/Semaphore/BSemaphore.cpp)
set(ShareMemSrc Tool/ShareMem/ShareMem.h
    Tool/ShareMem/ShareMem.cpp)
set(SqliteSrc Tool/Sqlite/SqliteDB.h
    Tool/Sqlite/SqliteDB.cpp)
set(StopWatchSrc Tool/StopWatch/StopWatch.h
    Tool/StopWatch/StopWatch.cpp)
set(StringSrc Tool/String/String.h
    Tool/String/String.cpp)
set(ThreadPoolSrc Tool/ThreadPool/ThreadPool.h
    Tool/ThreadPool/ThreadPool.cpp)
set(TimerSrc Tool/Timer/Timer.h
    Tool/Timer/Timer.cpp)
set(ZipSrc Tool/Zip/Zip.h
    Tool/Zip/Zip.cpp)

source_group(BaseEvent ${BaseEventSrc})
source_group(Foundation ${FoundationSrc})
source_group(ModuleLoader ${ModuleLoaderSrc})
source_group(Crypto ${CryptoSrc})
source_group(DateTime ${DateTimeSrc})
source_group(Directory ${DirectorySrc})
source_group(DllLoader ${DllLoaderSrc})
source_group(Encoding ${EncodingSrc})
source_group(File ${FileSrc})
source_group(Event ${EventSrc})
source_group(GUID ${GuidSrc})
source_group(Ini ${IniSrc})
source_group(Json ${JsonSrc})
source_group(Logger ${LoggerSrc})
source_group(PocoLogger ${PocoLoggerSrc})
source_group(OT ${OTSrc})
source_group(Pipe ${PipeSrc})
source_group(Process ${ProcessSrc})
source_group(Semaphore ${SemaphoreSrc})
source_group(ShareMem ${ShareMemSrc})
source_group(Sqlite ${SqliteSrc})
source_group(StopWatch ${StopWatchSrc})
source_group(String ${StringSrc})
source_group(ThreadPool ${ThreadPoolSrc})
source_group(Timer ${TimerSrc})
source_group(Zip ${ZipSrc})

# Set the library output path
if(WINDOWS)
    set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/build/${CMAKE_GENERATOR_PLATFORM}/${CMAKE_BUILD_TYPE})
else()
    set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/build/${CMAKE_BUILD_TYPE})
endif()

# Output library
add_library(BaseTool SHARED 
    ${BaseEventSrc}
    ${FoundationSrc}
    ${ModuleLoaderSrc}
    ${CryptoSrc}
    ${DateTimeSrc}
    ${DirectorySrc}
    ${DllLoaderSrc}
    ${EncodingSrc}
    ${FileSrc}
    ${EventSrc}
    ${GuidSrc}
    ${IniSrc}
    ${JsonSrc}
    ${LoggerSrc}
    ${PocoLoggerSrc}
    ${OTSrc}
    ${NotificationSrc}
    ${PipeSrc}
    ${ProcessSrc}
    ${SemaphoreSrc}
    ${ShareMemSrc}
    ${SqliteSrc}
    ${StopWatchSrc}
    ${StringSrc}
    ${ThreadPoolSrc}
    ${TimerSrc}
    ${ZipSrc}
)
